package logic

import (
	"DataSrv/global/model"
	"DataSrv/global/pkg"
	"context"
	"fmt"
	"gorm.io/gorm"

	"DataSrv/data"
	"DataSrv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ClassesVideoAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewClassesVideoAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ClassesVideoAddLogic {
	return &ClassesVideoAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ClassesVideoAddLogic) ClassesVideoAdd(in *data.ClassesVideoAddRequest) (*data.ClassesVideoAddResponse, error) {
	// todo: add your logic here and delete this line
	var classes model.Classes
	model.DB.Where("id = ?", in.ClassesId).Find(&classes)
	if classes.ID == 0 {
		return nil, fmt.Errorf("剧集不存在")
	}
	for _, id := range in.VideoIds {
		model.DB.Updates(&model.Video{
			Model: gorm.Model{
				ID: uint(id),
			},
			ClassesId: int(classes.ID),
		})
	}
	var ClassesLike []model.ClassesLike
	model.DB.Where("classes_id = ?", in.ClassesId).Find(&ClassesLike)
	datas := fmt.Sprintf("您追更的%s更新了", classes.Name)
	for _, like := range ClassesLike {
		key := fmt.Sprintf("user:%d:tips", like.UserId)
		rabbitmq := pkg.NewRabbitMQSimple("" +
			key)
		rabbitmq.PublishSimple(datas)
	}
	return &data.ClassesVideoAddResponse{}, nil
}
